package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Users;
import db.dao.UsersDAO;
/*添加管理员*/
public class AddManageAction extends ActionSupport{
	private String name="";	//用户名
	private String pwd1="";	//新密码
	private String pwd2="";	//确认密码
	
	public AddManageAction() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd1() {
		return pwd1;
	}

	public void setPwd1(String pwd1) {
		this.pwd1 = pwd1;
	}
	
	public String getPwd2() {
		return pwd2;
	}

	public void setPwd2(String pwd2) {
		this.pwd2 = pwd2;
	}

	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//用户名不能为空，密码不能为空且2次必须相同
		if (!name.equals("")&&!pwd1.equals("")&&pwd1.equals(pwd2)) {
			//设置用户参数
			UsersDAO uDao = new UsersDAO();
			Users users = new Users();
			users.setUsername(name);
			users.setUserpwd(pwd1);
			users.setUsertype("管理");
			//保存
			result = uDao.save(users);
			if (result.equals("success")) {
				//保存成功弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('添加成功!')</script>");
	            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}else {
				//保存失败弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('添加失败!')</script>");
	            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}
		}else {
			//填写错误弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
